package com.xy.shop.common.utils;

import com.xy.shop.common.constant.WebConstants;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

/**
*@Description: web 工具类
*@Param:  null
*@Author: zyy
*@date: 2019/12/14
*@Time: 17:20
*/
@Component
public class WebUtil {

    @Autowired
    private StringRedisTemplate redisTemplate;

    public  String getAppCode(String loginName){
        return this.getAppValue(loginName,1);
    }

    public  String getAppName(String loginName){
        return this.getAppValue(loginName,2);
    }

    public int getAppId(String loginName){
        int appId =0;
        String appValue = this.getAppValue(loginName, 0);
        if(StringUtils.isNotBlank(appValue)){
            appId = Integer.parseInt(appValue);
        }
        return appId;
    }

    private String getAppValue(String loginName,int index){
        String appValue = null;
        String value = redisTemplate.opsForValue().get(WebConstants.RedisKeyPrefix.APPID+loginName);
        if(StringUtils.isNotBlank(value)) {
            try {
                String[] v =value.split(",");
                appValue=v[index];
            }catch (Exception e){
                e.printStackTrace();
            }
        }else {
            Subject subject = SecurityUtils.getSubject();
            subject.logout();
        }
        return appValue;
    }
}
